<?php
/**
 * MessageAction 消息模块.
 *
 * @version TS3.0
 */
class VisitAction extends Action
{
    /**
     * 模块初始化.
     */
    public function _initialize()
    {
    }

    /**
     * 私信列表.
     */
    public function index()
    {
        $today = strtotime(date('Y-m-d'));
        $token = (isset($_GET['token']) && $_GET['token'] ) ? trim($_GET['token']) : '1111111111111111';
        $url   = isset($_GET['url'])   ? $_GET['url']   :'/';
        $rs = d('online_status_uv')->where(['day'=> $today ,'token'=>$token,'url'=>$url])->find();
        if(!$rs){
            $data['url']   = $url;
            $data['token'] = $token;
            $data['day'] = $today;
            $data['ip'] = $this->getClientIp();
            $data['addtime'] = time();
            d('online_status_uv')->add($data);
            $rs = $this->doOnlineLog('online',2);
        }
        $ip = $this->getClientIp();
        if($ip){
            $rs2 = d('online_status_uv')->where(['day'=> $today ,'ip'=>$ip ])->find();
            if(!$rs2){
                $this->doOnlineLog('online',3);
            }
        }

        $this->doOnlineLog('online',1);
        $this->ajaxReturn( $rs , 'success', 200);
    }

    public function getFeedConfig(){
        $t1['is_set_default'] = 0 ;
        $t1['is_need_check'] = 0 ;
        $t2 = 0;
        $topic_config = model('Xdata')->get('admin_Config:topic');
        if($topic_config){
            $t1['is_set_default'] = $topic_config['is_set_default'] ;
            $t1['is_need_check']  = $topic_config['is_need_check'] ;
        }
        $feed_config  = model('Xdata')->get('admin_Config:feed');
        if($topic_config){
            if( is_array($feed_config) &&  in_array( 'audit', $feed_config['weibo_premission'])){
                $t2 = 1;
            }

        }
        $return['topic_config'] = $t1;
        $return['feed_config']  = $t2;
        $this->ajaxReturn($return,'success',200);
    }

    //后台内容分析：content_stats 表的统计数据
    public function doOnlineLog($action = 'online' , $type = 1 , $res = 0){
        if($action == 'online'){
            $date = date('Y-m-d',time());
            $rs = d('online_stats')->where(['day'=> $date])->find();
            if(!$rs){
                $data['day'] = $date;
                $data['most_online_guests'] = 0;
                $data['most_online_time'] = 0;
                $data['most_online'] = 0;
                $data['uv'] = 0;
                $data['pv'] = 0;
                $data['ip'] = 0;
                $data['total_pageviews'] = 0;
                $data['most_online_users'] = 0;
                $data['total_guests'] = 0;
                $data['total_users'] = 0;
                $res =  d('online_stats')->add($data);
            }

            if($type == 1){
                $res =  d('online_stats')->setInc('pv', array('day' => $date), 1);
            }else if($type == 2){
                $res =  d('online_stats')->setInc('uv', array('day' => $date), 1);
            }else if($type == 3){
                $res =  d('online_stats')->setInc('ip', array('day' => $date), 1);
            }

        }
        return $res;
        //$this->ajaxReturn( $res , 'success', 200);
    }

    public function getClientIp()
    {
        if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $ip = getenv('REMOTE_ADDR');
        } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $ip = $_SERVER['REMOTE_ADDR'];
        } else {
            $ip = rand(110000,99999999);
        }

        return addslashes($ip);
    }

}
